package com.fz.study.project.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.fz.study.project.entity.AdminUser;
import com.fz.study.project.entity.dto.LoginDto;
import com.fz.study.project.entity.Result;
import com.fz.study.project.exception.BizException;
import com.fz.study.project.mapper.AdminUserMapper;
import com.fz.study.project.service.IAdminUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fz.study.project.utils.JwtUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-02-05
 */
@RequiredArgsConstructor
@Slf4j
@Service
public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUser> implements IAdminUserService {
    @Override
    public Result loginByName(LoginDto loginDto) {
        Optional<LoginDto> loginOptional = Optional.ofNullable(loginDto);
        String username = loginOptional.map(LoginDto::getUsername).orElse("");
        String password = loginOptional.map(LoginDto::getPassword).orElse("");
        if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password)){
            BizException.throwBizException("用户名或密码不能为空");
        }
        AdminUser user = lambdaQuery().eq(AdminUser::getUsername, username).one();
        if (Objects.isNull(user)){
            BizException.throwBizException("用户名不存在");
        }
        if (!password.equals(user.getPassword())){
            BizException.throwBizException("密码输入不正确");
        }
        Map<String,Object> claims = new HashMap<>();
        claims.put("username",username);
        claims.put("password",password);
        StpUtil.login(user.getUsername());
//        生成jwt令牌并返回给前端
        return Result.success(JwtUtils.generateJwt(claims));
    }

    @Override
    public void saveByName(AdminUser user) {
        if (Objects.isNull(user)){
            BizException.throwBizException("用户名为空");
        }
        String username = user.getUsername();
        AdminUser count = lambdaQuery().eq(AdminUser::getUsername, username).one();
        if (!Objects.isNull(count)){
            BizException.throwBizException("用户已经存在，请重新输入！");
        }
        save(user);
    }

    @Override
    public void updateByUser(AdminUser user) {
        String username = user.getUsername();
        String password = user.getPassword();
        if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password)){
            BizException.throwBizException("用户名或密码不能为空");
        }
        AdminUser userCount = lambdaQuery().eq(AdminUser::getUsername, username).eq(AdminUser::getPassword,password).one();
        if (!Objects.isNull(userCount)){
            BizException.throwBizException("修改数据与原数据一致，不用修改！");
        }
        updateById(user);
    }

    @Transactional
    @Override
    public void deleteByIds(List<Integer> ids) {
        ids.forEach(id->{
            if (Objects.isNull(lambdaQuery().eq(AdminUser::getId,id).one())){
                BizException.throwBizException("想要删除的id不存在！");
            }
        });
        removeBatchByIds(ids);
    }

    @Override
    public void updateWithPassword(AdminUser adminUser) {
        if (Objects.isNull(adminUser)){
            BizException.throwBizException("修改数据不能为空！");
        }
        log.info("前端传递的数据为：{}",adminUser);
        AdminUser one = lambdaQuery().eq(AdminUser::getUsername, adminUser.getUsername()).one();
        log.info("获取到的系统人员信息为：{}",one);
        if (Objects.isNull(one)){
            BizException.throwBizException("用户不存在！");
        }
        updateById(adminUser);
    }
}
